import { defineStore, createPinia } from "pinia";
import { GlobalState } from "./interface";
import piniaPersistConfig from "@/config/piniaPersist";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";

// defineStore 调用后返回一个函数，调用该函数获得 Store 实体
export const GlobalStore = defineStore({
	// id: 必须的，在所有 Store 中唯一
	id: "GlobalState",
	// state: 返回对象的函数
	state: (): GlobalState => ({
		// token
		token: "",
    }),
	getters: {},
	actions: {
		// setToken
		async setToken(token: string) {
			this.token = token;
		},
	},
	persist: piniaPersistConfig("GlobalState")
});

// piniaPersist(持久化)
const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);

// 使用示例
/* 
* 页面引入： import { GlobalStore } from "@/store";
* setup： const globalStore = GlobalStore();
* 存储：globalStore.setToken(token);
* 获取: globalStore.token
*/

export default pinia;
